const fs = require('fs');

module.exports = () => async (ctx, next) => {
  const startTime = Date.now();
  const requestTime = new Date();
  await next();
  const ms = Date.now - startTime;
  let logout = `${ctx.request.ip}--${requestTime}--${ctx.method}--${ctx.url}--${ms}ms`;
  // 输出日志文件
  fs.appendFileSync('./log.txt', logout + '\n');
};

// app.js
const Koa = require('koa');
const app = new Koa();
const logger = require('./middleware/logger');
app.use(logger());
app.listen(3000, () => {
  console.log('server is running at http://localhost:3000');
});
